package sifApp.sifAppDataBaseHelper;
/*
 * 
 */
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import sifApp.sifAppStatic.Global;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * The Class BarcodeReaderDBHelper.
 * 
 * @author Tam-LT
 */
public class SIFDataHelper extends SQLiteOpenHelper {
    /****
     * path to save database in real device
     */
    private final String DB_PATH = "/data/data/%s/databases/";
    /*******
     * database name
     */
    private final static String DB_NAME = Global.DATABASE_NAME;
    private Context myContext;
    private static SIFDataHelper instance = null;
    private String TAG = "TAG";
    private String package_name;

    /**
     * Gets the single instance of BarcodeReaderDBHelper.
     * 
     * @param context the context
     * @return single instance of BarcodeReaderDBHelper
     */
    public static SIFDataHelper getInstance(Context context, String packageName) {
        if (instance == null) {
            instance = new SIFDataHelper(context, packageName);
        }
        return instance;
    }

    /**
     * Instantiates a new barcode reader db helper.
     * 
     * @param context the context
     * @param packageName
     */
    public SIFDataHelper(Context context, String packageName) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
        this.package_name = packageName;
    }

    /*
     * (non-Javadoc)
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
     * .SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    /*
     * (non-Javadoc)
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
     * .SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    /*
     * (non-Javadoc)
     * @see android.database.sqlite.SQLiteOpenHelper#close()
     */
    @Override
    public synchronized void close() {
        super.close();
    }

    /**
     * Check and copy database.
     */
    public void checkAndCopyDatabase() {
        boolean dbExist = checkDataBase();
        if (dbExist) {
            Log.d(TAG, "database already exist!");
        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.d(TAG, "Error copying database");
            }
        }
    }

    /**
     * Check data base.
     * 
     * @return true, if successful
     */
    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = String.format(DB_PATH, package_name) + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {
        }
        if (checkDB != null)
            checkDB.close();
        return checkDB != null ? true : false;
    }

    /**
     * Copy data base.
     * 
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = String.format(DB_PATH, package_name) + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    /**
     * Open data base.
     * 
     * @return the sQ lite database
     * @throws SQLException the sQL exception
     */
    public SQLiteDatabase openDataBase() throws SQLException {
        String myPath = String.format(DB_PATH, package_name) + DB_NAME;
        SQLiteDatabase sqliteDataBase = SQLiteDatabase.openDatabase(myPath,
                null, SQLiteDatabase.OPEN_READWRITE);
        return sqliteDataBase;
        // return getWritableDatabase();
    }
}

